package com.chenyao.bookstore.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.chenyao.bookstore.datahandler.IUserDataHandler;
import com.chenyao.bookstore.datahandler.impl.UserDataHandlerImpl;
import com.chenyao.bookstore.entity.User;
import com.chenyao.bookstore.util.Constants;
import com.chenyao.bookstore.util.Util;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
	private static final String METHOD_LOGIN = "login";
	private static final String METHOD_REGISTER = "register";
	private static final long serialVersionUID = 1L;
	private static final String WEB_PARAMETER_CONFORMPWD = "conformPwd";
	private static final String WEB_PARAMETER_PASSWORD = "password";

	private static final String WEB_PARAMWTER_USERNAME = "username";

	private IUserDataHandler userDataHandler;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UserServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException,
			IOException {
		this.process(request, response);
	}

	private void doLogin(final HttpServletRequest request, final HttpServletResponse response) throws IOException,
			ServletException {
		String username = request.getParameter(UserServlet.WEB_PARAMWTER_USERNAME);
		String password = request.getParameter(UserServlet.WEB_PARAMETER_PASSWORD);
		// 这同样需要验证用户名，密码的有效性以及合法性。时间仓促我就不写了，陈窈记得加上
		User user = new User();
		user.setUsername(username.toLowerCase());
		user.setPassword(password);
		user = this.userDataHandler.login(user);
		if (null != user) {
			// 登陆成功
			request.getSession().setAttribute("loginUser", user);
			request.getRequestDispatcher("WEB-INF/index.jsp").forward(request, response);
		} else {
			response.sendRedirect("login.jsp");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(final HttpServletRequest request, final HttpServletResponse response)
			throws ServletException, IOException {
		this.process(request, response);
	}

	private void doRegister(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
		String username = request.getParameter(UserServlet.WEB_PARAMWTER_USERNAME);
		String password = request.getParameter(UserServlet.WEB_PARAMETER_PASSWORD);
		String conformPwd = request.getParameter(UserServlet.WEB_PARAMETER_CONFORMPWD);
		// 这同样需要验证用户名，密码，确认密码的有效性以及合法性。时间仓促我就不写了，陈窈记得加上

		User user = new User();
		user.setUsername(username.toLowerCase());
		user.setPassword(password);
		JSONObject jsonObj = new JSONObject();
		if (this.userDataHandler.register(user)) {
			// 注册成功
			jsonObj.put("code", 1);
			jsonObj.put("message", "successful !");

		} else {
			// 注册失败
			jsonObj.put("code", 0);
			jsonObj.put("message", "failed !");
		}
		Util.sendData2Client(response, jsonObj.toString(), true);
	}

	@Override
	public void init() throws ServletException {
		super.init();
		this.userDataHandler = new UserDataHandlerImpl();
	}

	private void process(final HttpServletRequest request, final HttpServletResponse response) throws IOException,
			ServletException {
		String method = request.getParameter(Constants.WEB_PARAMETER_METHOD);
		if (UserServlet.METHOD_REGISTER.equals(method)) {
			this.doRegister(request, response);
		} else if (UserServlet.METHOD_LOGIN.equals(method)) {
			this.doLogin(request, response);
		} else {
			request.getRequestDispatcher("WEB-INF/index.jsp").forward(request, response);
		}
	}
}
